package com.chaoyong.higo.adapter;

import java.util.List;
import java.util.Timer;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.bean.NewsAnnoBean;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.chaoyong.higo.view.CountdownView;
import com.chaoyong.higo.view.CustomDigitalClock;
import com.chaoyong.higo.view.CustomDigitalClock.ClockListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewsAdapter extends BaseAdapter<NewsAnnoBean.DataEntity.ListEntity> {

	private TextView name_tx1;
	private ImageView news_name_img1;
	private CustomDigitalClock news_name_time1;
	private TextView news_name_time2;
	private Activity context;

	private MyCountDownTimer mc;
	private CountdownView cv_countdownView;

	private int recLen = 0;

	// Handler handler = new Handler();

	public NewsAdapter(Context context) {
		super(context);
		this.context = (Activity) context;
	}

	// @Override
	// public int getCount() {
	// return 4;
	// }

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_home_new2, null);
		}
		findView(convertView);

		String goodsName = mList.get(position).getGoods_name();
		List<String> uri = mList.get(position).getCover_img();
		String open_time = mList.get(position).getOpen_time();
		String expect = mList.get(position).getExpect();

		/*
		 * for (recLen = 0; recLen < 100; recLen++) { new
		 * Handler().postDelayed(new Runnable() {
		 * 
		 * @Override public void run() { // TODO Auto-generated method stub
		 * context.runOnUiThread(new Runnable() {
		 * 
		 * @Override public void run() { // TODO Auto-generated method stub
		 * news_name_time2.setText(recLen+""); } }); } }, 1000); }
		 */

		/*
		 * long time3 = (long)9 * 60 * 60 * 1000; cv_countdownView.start(time3);
		 */

		if (!EmptyUtil.isEmpty(goodsName)) {
			name_tx1.setText("(第" + expect + context.getResources().getString(R.string.expect) + ")" + goodsName);
		}
		if (!EmptyUtil.isEmpty(uri)) {
			ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uri.get(0), news_name_img1);
		}

		if (!EmptyUtil.isEmpty(open_time)) {

			long times = Long.parseLong(open_time);
			long curTime = System.currentTimeMillis();
			/*
			 * Log.e("time", curTime + ""); news_name_time1.setEndTime((times +
			 * 60) * 1000); news_name_time1.setClockListener(new ClockListener()
			 * {
			 * 
			 * @Override public void timeEnd() {
			 * 
			 * }
			 * 
			 * @Override public void remainFiveMinutes() {
			 * 
			 * } });
			 */

			long time3 = ((times + 60) * 1000) - curTime;
			cv_countdownView.start(time3);
			if (time3 == 0) {
				cv_countdownView.setVisibility(View.GONE);
				news_name_time2.setVisibility(View.VISIBLE);
			}

		} else {
			// news_name_time1.setText("已揭晓");
			cv_countdownView.setVisibility(View.GONE);
			news_name_time2.setVisibility(View.VISIBLE);
		}

		/*
		 * new Thread() { public void run() { //这儿是耗时操作，完成之后更新UI；
		 * context.runOnUiThread(new Runnable(){
		 * 
		 * @Override public void run() { //更新UI
		 * 
		 * //news_name_time2.setText("fdf"); for (int i = 0; i < 999; i--) { try
		 * { Thread.sleep(1000); } catch (InterruptedException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 * news_name_time2.setText(i+""); if(i == 0){ i = 999; } } }
		 * 
		 * }); } }.start();
		 */

		/*
		 * for (int i = 0; i < 999; i--) { try { Thread.sleep(1000); } catch
		 * (InterruptedException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } news_name_time2.setText(i+""); if(i == 0){ i =
		 * 999; } }
		 */

		/*
		 * new CountDownTimer(30000, 100) { public void onTick(long
		 * millisUntilFinished) { news_name_time2.setText(": " +
		 * millisUntilFinished / 1000); notifyDataSetChanged(); } public void
		 * onFinish() { news_name_time2.setText("0"); } }.start();
		 */

		return convertView;
	}

	private void findView(View convertView) {
		name_tx1 = ViewHolder.get(convertView, R.id.news_name_tx1);
		news_name_img1 = ViewHolder.get(convertView, R.id.news_name_img1);
		// news_name_time1 = ViewHolder.get(convertView, R.id.news_name_time1);
		news_name_time2 = ViewHolder.get(convertView, R.id.news_name_time8);
		cv_countdownView = ViewHolder.get(convertView, R.id.cv_countdownViewTest3);
	}

	/**
	 * 继承 CountDownTimer 防范
	 * 
	 * 重写 父类的方法 onTick() 、 onFinish()
	 */

	class MyCountDownTimer extends CountDownTimer {
		/**
		 * 
		 * @param millisInFuture
		 *            表示以毫秒为单位 倒计时的总数
		 * 
		 *            例如 millisInFuture=1000 表示1秒
		 * 
		 * @param countDownInterval
		 *            表示 间隔 多少微秒 调用一次 onTick 方法
		 * 
		 *            例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
		 * 
		 */
		public MyCountDownTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			news_name_time2.setText("done");
		}

		@Override
		public void onTick(long millisUntilFinished) {
			Log.i("MainActivity", millisUntilFinished + "");
			news_name_time2.setText(":" + millisUntilFinished / 1000);
		}
	}

}
